|
Table of Contents
1. The Story
of C++! (The basics for any beginner!)
2. The A reference document on the basic elements of C++.
3. The Patterns
|
Once again we would use the our "While Loop With Priming
Read" pattern. The code code (for the 'DisplayInfo' option) would look as follows:
VI. The Function 'DisplayInfo'
A. Class Instances as Parameters
Note how we use 'Contract' (the name of the class) where a type
name goes. Since we are declaring a parameter here and the system
knows that this local instance will be immediately instantiated
with the property values of some contract on the calling side,
this usage is OK. What actually happens is that the properties
of the actual parameter are copied to the corresponding data members
in the local instance.Later we will see situations in which it
is necessary to explicitly declare and define what is called a copy constructor
It should be emphasized that it would not be valid to, for example,
declare a local variable here (or anywhere else) as: "Contract
c1;". Upon compiling you would get an error message similar
to:
In other words, there is no constructor for the class 'Contract'
that has no parameters. This, of course, is true since we only
declared one constructor for the Contract class and it has three
parameters.
B. The Code for DisplayInfo
We have added three 'endl' outputs at the end to cause three lines
to be skipped on the monitor between this output and whatever
happens next. This simply makes the output more readable.
There is a slightly shorter way to write this code. Functions
that return values can be placed anywhere a variable can be placed
as long as one is careful to match types. For example then, one
could drop the variable 'sqFootage' and replace it directly with
the function call as in:
Using this approach the value returned by 'ProvideSquareFootage'
is sent directly to the output stream instead of being temporarily
stored in a local variable whose value is then sent to the stream.
You should use whichever approach feels best to you. (Note that
in this version the function call does not end with a semi-colon
because it is part of a larger instruction.)
VII. The Function 'ChangeInfo'
A. Returning a More Complex Data Type
If we did this we would have to rewrite the calls to "ChangeInfo'
in 'ProcessUserChoices' to, for example:
While this would work, it is not as commonly done as a second
way. In chapter four we learned how to use the '&' symbol
to allow more than one value to be returned. We noted further
that the '&' symbol creates a "two way transmission path"
allowing information to not only be returned but also sent via
the same connection. In the case of 'ChangeInfo' we already need
a path to transmit information from the calling function (in other
words, we need to receive a contract) so why not simply use the
same path to return the changed contract. The '&' does this.
Here is the new declaration:
To emphasize the key point: The '&' symbol creates a two way
path so that the original information about the contract in question
can be received by 'ChangeInfo' and the changed information can
be returned. When a call to 'ChangeInfo' such as:
in 'ProcessUserChoices' is completed the information originally
in contract1 has been sent to 'ChangeInfo' and the same information,
as modified by 'ChangeInfo' has been sent back. The instance 'contract1'
in 'ProcessUserChoices' has now been modified.
B. The Code
One other point: The while test condition is a bit different in
that it uses the 'AND' symbol ('&&'). Students are often
confused about when to use AND and OR. The confusion comes partially
from the way a situation like this is commonly described in English.
The specifications are likely to say something like:
While the specifications statement has an 'or' in it, it gets
translated as 'AND' or '&&'. The reason is that the while
statement is not concerned with when to stop as when to continue.
Remember, the while statement test condition can be translated
as:
In other words, when translating a phrase such as Stop when the
user enters 'N' or 'n" into a C++ while statement, it might
be wise to first translate the English into, "while the user
has not entered 'N' and has not entered 'n'....". Note how
this English captures the meaning of the C++ 'while' statement
and, therefore, the translation process (from algorithm to code)
is relatively straight forward. The moral of the story is be very
careful how you translate English 'and' and 'or' into '&&'
and '||'.
VIII. Putting It All Together
Once you have finished designing and coding this program yourself,
you are encouraged to read carefully the code
provided here. If you discover differences,
do not automatically assume that your design or code needs to
be changed. There is more than way to write a program. Analyze
the differences you find and ask the following questions:
Do your design and code represent a different way of interfacing
with the user?
Do your design and code handle the same situations or possible
problems? Does they handle more situations or less? For example,
which design does a better job of handling incorrect user entries?
VIII. Final Thought
|